/*
 * JPushReceiver.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.yidiandao.business.data.push.PushProcessManager;
import com.yidiandao.business.utils.LogUtils;
import com.yidiandao.business.utils.UIUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**
 * 极光推送的Receiver
 * <p>
 * <p>如果不定义这个 Receiver，则： 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class JPushReceiver extends BroadcastReceiver {

    /**
     * 打印所有的 intent extra 数据
     */
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
                    LogUtils.i("This message has no Extra data");
                    continue;
                }

                LogUtils.i("推送 Extra: " + bundle.getString(JPushInterface.EXTRA_EXTRA));

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next();
                        sb.append("\nkey:" + key + ", value: [" + myKey + " - " + json.optString(
                                myKey) + "]");
                    }
                } catch (JSONException e) {
                    LogUtils.e("Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent == null || intent.getExtras() == null) {
            return;
        }

        Bundle bundle = intent.getExtras();
        LogUtils.d("onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            // 自定义消息。只会收到消息。后续操作需要开发者自己处理
            PushProcessManager manager =
                    new PushProcessManager(context, bundle, UIUtils.isForeground());
            manager.processPushMessage();
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            // 极光通知,会自动弹出系统通知
            LogUtils.d("接收到推送下来的通知");
        } /*else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
              LogUtils.d("用户点击打开了通知");

              String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);

              //打开自定义的Activity
          }*/
    }
}
